package org.shj.ws;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import javax.xml.namespace.QName;

/**
 * Webservice 的客户端，顺便演示解析 xml 的框架
 * @author Shen Huang Jian
 * @date 2020-08-06 9:16
 */
public class WsClient {

    public static void main(String[] args){

        String xml = "关羽";
        Object[] params = new Object[]{xml};
        RPCServiceClient serviceClient;
        try {
            serviceClient = new RPCServiceClient();
            Options options = serviceClient.getOptions();
            EndpointReference reference = new EndpointReference("http://localhost:8088/ws/services/HelloWorld");
            options.setTo(reference);
            //namespaceUrl 应该与 webservice-server 中的 server-config.wsdd 中的  <namespace> 一致
            // <service name="HelloWorld" provider="java:RPC" >
            //        <parameter name="className" value="example.HelloWorld"/>
            //        <parameter name="allowedMethods" value="*"/>
            //        <parameter name="scope" value="Application"/>
            //        <namespace>http://org.shj.ws.example</namespace>
            //    </service>
            String namespaceUrl = "http://org.shj.ws.example";
            String methodName = "sayHelloWorldFrom";
            QName name = new QName(namespaceUrl, methodName);
            Object[] re = serviceClient.invokeBlocking(name, params,  new Class[]{String.class});

            System.out.println(XmlUtil.parseMessageFromWs(re[0].toString()));


            //有可能是新建的

            /*xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<VehicleInfo>" +
                    "<deviceNo></deviceNo>" +
                    "<count></count>" +
                    "<beginTime></beginTime>" +
                    "<endTime></endTime>" +
                    "<violationCode></violationCode>" +
                    "<withPic>false</withPic>" +
                    "<roadCode></roadCode>" +
                    "<plateColor></plateColor>" +
                    "<vechicleTypeCaption></vechicleTypeCaption>" +
                    "<alarmType></alarmType>" +
                    "<areaCode></areaCode>" +
                    "<needPage>true</needPage>" +
                    "<currentPage>1</currentPage>" +
                    "<pageSize>10</pageSize>" +
                    "<orderType></orderType>" +
                    "</VehicleInfo>";
            serviceClient = new RPCServiceClient();
            Options options = serviceClient.getOptions();
            EndpointReference reference = new EndpointReference("http://localhost:8088/test/services/HelloWorld");
            options.setTo(reference);
            QName name = new QName("http://example", "sayHelloWorldFrom");
            Object[] re = serviceClient.invokeBlocking(name, params,  new Class[]{String.class});

            ResponseInfo responseInfo = XmlUtil.parseMessageFromWs((String)re[0]);
            System.out.println(responseInfo);*/

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
